package Actions;

import Beans.Inventory;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import io.restassured.response.Response;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.testng.Assert;

import static io.restassured.RestAssured.given;

public class WMSActions extends BaseAction{
    public SMSActions sms=new SMSActions();
    String warehouseid;
    public Response getSHInfo(String vhid, String no){

        response=given()
                .cookies(cookies)
                .headers("x-yh-wms-whid",vhid)
                .contentType("application/json")
                .body("{\n" +
                        "\t\"pagesize\": 50,\n" +
                        "\t\"pageindex\": 1,\n" +
                        "\t\"threeno\": \""+no+"\"\n" +
                        "}")
                .post(URL+"wms-ibd-hub/receipt/list");

        return response;
    }

    public String getSHStatus(Response res){

        String shStatus=res.jsonPath().getString("data.rows[0].receiptstatusname");
        return shStatus;
    }

    public Response wmsReceive(String shopcode,String billno){
        String warehouseid=getWarehouseID(shopcode);
        response=getSHInfo(warehouseid,billno);
        String receiptid=response.jsonPath().getString("data.rows[0].id");
        String receiptno=response.jsonPath().getString("data.rows[0].receiptno");
        Assert.assertNotNull(receiptid,"没有找到单据"+billno+"的收货单！！");
        log.info("单据"+billno+"收货单号为："+receiptno);
        response=enterSHBill(warehouseid,receiptid);
        String receiptdetailid=response.jsonPath().getString("data.rows[0].receiptdetailid");
        String skunumber=response.jsonPath().getString("data.rows[0].expectedqty");
        Assert.assertNotNull(receiptdetailid);
        log.info("进入收货单收货。。。");
        response=receiveSKU(warehouseid,receiptdetailid,skunumber);
        String result=response.jsonPath().getString("data.result");


        Assert.assertEquals(result,"0","收货失败！");
        log.info("收货单"+receiptno+"收货完成！");

        response=closeSHBill(warehouseid,receiptid);
        String shStatus=getSHStatus(getSHInfo(warehouseid,billno));
        Assert.assertEquals(shStatus,"已关闭","收货单状态不正确！");
        log.info("收货单"+receiptno+"已关闭");

        int fail=10;
        String status=sms.getBillStatus(sms.getBillInfo(billno));
        while(!("已关闭".equals(status))&&fail>0){
            fail--;
            log.info("单据"+billno+"状态有误，等待WMS回传,10秒后重新查询......");
            waitSecond(10);
            status=sms.getBillStatus(sms.getBillInfo(billno));
        }
        Assert.assertEquals(status,"已关闭","单据"+billno+"状态不正确！");
        log.info("单据"+billno+"已关闭");

        return response;
    }

    //进入收货单
    public Response enterSHBill(String Warehouseid,String receiptid){
        response= given()
                .cookies(cookies)
                .contentType("application/json")
                .headers("x-yh-wms-whid",Warehouseid)
                .body("{\n" +
                        "\t\"receiptid\": \""+receiptid+"\"\n" +
                        "}")
                .post(URL+"wms-ibd-hub/receiptrecord/listeditor");

        return response;
    }
    //进行收货操作
    public Response receiveSKU(String Warehouseid,String receiptdetailid,String skunumber){
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .headers("x-yh-wms-whid",Warehouseid)
                .body("{\n" +
                        "\t\"receiptdetailid\": \""+receiptdetailid+"\",\n" +
                        "\t\"receiptnum\": \""+skunumber+"\"\n" +
                        "}")
                .post(URL+"wms-ibd-hub/fastreceipt/receipt");
        return response;

    }

    //关闭收货单
    public Response closeSHBill(String Warehouseid,String receiptid){
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .headers("x-yh-wms-whid",Warehouseid)
                .body("{\n" +
                        "\t\"receiptids\": [\""+receiptid+"\"]\n" +
                        "}")
                .post(URL+"wms-ibd-hub/receipt/close");
        return response;

    }
    public String getWarehouseID(String shopcode) {
        response=given()
                .cookies(cookies)
                //.headers("x-yh-wms-whid","162249205597896704")
                .contentType("application/json")
                .body("{\"pagesize\":50,\"pageindex\":1,\"code\":\""+shopcode+"\"}")
                .post(URL+"wms-mst-center/warehouse/getlist");
        String Warehouseid=response.jsonPath().getString("data.rows[0].id");
        return Warehouseid;


    }
    //获取出库单信息
    public Response getCKInfo(String billno){
        response=sms.getBillInfo(billno);
        String outAddress=null;
        if('F'==billno.charAt(1)){
            outAddress=response.jsonPath().getString("data.rows[0].addressoutcode");
        }else if('O'==billno.charAt(1)&&"2".equals(response.jsonPath().getString("data.rows[0].potype"))){
             outAddress=response.jsonPath().getString("data.rows[0].addresscode");
        }else {
            log.info(billno+"该类型单据不需要出库!");
        }
        Assert.assertNotEquals(outAddress,null,"查询出库地点失败！");
             warehouseid=getWarehouseID(outAddress);

        response=given()
                .cookies(cookies)
                .headers("x-yh-wms-whid",warehouseid)
                .contentType("application/json")
                .body("{\n" +
                        "\t\"pagesize\": 50,\n" +
                        "\t\"pageindex\": 1,\n" +
                        "\t\"no\": \""+billno+"\"\n" +
                        "}")
                .post(URL+"wms-obd-center/order/oswarehouse/list");

        return response;


    }

    //新建波次
    public Response createWave(String billno){
        response=getCKInfo(billno);
        String companyid=response.jsonPath().getString("data.rows[0].companyid");
        String carrierid=response.jsonPath().getString("data.rows[0].carrierid");
        String orderid=response.jsonPath().getString("data.rows[0].id");
        response=given()
                .headers("x-yh-wms-whid",warehouseid)
                .cookies(cookies)
                .contentType("application/json")
                .body("{\n" +
                        "\t\"orderids\": [\""+orderid+"\"],\n" +
                        "\t\"waveflowid\": \"163598907501219840\",\n" +
                        "\t\"ordertype\": \"2\",\n" +
                        "\t\"orderbusinesstype\": \"8\",\n" +
                        "\t\"companyid\": \""+companyid+"\",\n" +
                        "\t\"carrierid\": \""+carrierid+"\"\n" +
                        "}")
                .post(URL+"wms-obd-hub/wave/create");

        String waveno=response.jsonPath().getString("data.list[0]");
        Assert.assertNotEquals(waveno,null,"新建波次失败!");
        log.info("新建波次号成功!波次号:"+waveno);
        allotInventory(waveno);

        return response;
    }

    //分配库存，完成出库
    public void allotInventory(String waveno){
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .headers("x-yh-wms-whid",warehouseid)
                .body("{\n" +
                        "\t\"pagesize\": 50,\n" +
                        "\t\"pageindex\": 1,\n" +
                        "\t\"waveno\": \""+waveno+"\"\n" +
                        "}")
                .post(URL+"wms-obd-center/wave/list");
        String id=response.jsonPath().get("data.rows[0].id");
        Assert.assertNotEquals(waveno,null,"新建波次失败!");
        log.info("波次号:"+waveno+"的波次ID为:"+id);
                 given()
                         .cookies(cookies)
                         .contentType("application/json")
                         .headers("x-yh-wms-whid",warehouseid)
                         .body("{\n" +
                                 "\t\"waveids\": [\""+id+"\"]\n" +
                                 "}")
                         .post(URL+"wms-obd-hub/wave/distributionwaves");
    }

    //获取出库单状态
    public String getCKStatus(Response res){
        return res.jsonPath().getString("data.rows[0].orderstatusname");


    }

    //快速验收
    public Response quickDelivery(String billno){
        response=sms.getBillInfo(billno);
        if ("O".equals(billno.charAt(1))&&"2".equals(response.jsonPath().getString("data.rows[0].potype"))){
          log.info("该类型单据无需入库");
          return null;
        }
        System.out.println(response.asString());
        String addressincode=null;

        if ('F'==billno.charAt(1)) {
            addressincode = response.jsonPath().getString("data.rows[0].addressincode");
        }else{
            addressincode = response.jsonPath().getString("data.rows[0].addresscode");
        }
        Assert.assertNotEquals(null,addressincode,"入库地点错误!");
        log.info("单据"+billno+"收货地点为"+addressincode);

        warehouseid=getWarehouseID(addressincode);
        System.out.println(warehouseid);
        response=getSHInfo(warehouseid,billno);
        String receiptid=response.jsonPath().getString("data.rows[0].id");

        Double skunumber= Double.valueOf(response.jsonPath().getString("data.rows[0].skuqtycount"));
        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .headers("x-yh-wms-whid",warehouseid)
                .body("{\n" +
                        "\t\"receiptid\": \""+receiptid+"\"\n" +
                        "}")
                .post(URL+"wms-ibd-hub/receiptrecord/fastacceptancelisteditor");
        String locationid=response.jsonPath().getString("data.rows[0].locationid");
        String receiptdetailid=response.jsonPath().getString("data.rows[0].receiptdetailid");

        response=given()
                .cookies(cookies)
                .contentType("application/json")
                .headers("x-yh-wms-whid",warehouseid)
                .body("{\n" +
                        "\t\"receiptid\": \""+receiptdetailid+"\",\n" +
                        "\t\"fastacceptancereceiptdetailbodylist\": [{\n" +
                        "\t\t\"receiptdetailid\": \""+receiptdetailid+"\",\n" +
                        "\t\t\"receiptnum\": "+skunumber+",\n" +
                        "\t\t\"locationid\": \""+locationid+"\",\n" +
                        "\t\t\"starttime\": \"2018-10-29T16:00:00.000Z\"\n" +
                        "\t}]\n" +
                        "}")
                .post(URL+"wms-ibd-hub/fastacceptancereceipt/receipt");
        return response;
    }

    //查询全渠道信息
    public  Inventory queryInventory(String sitecode, String skucode){
        Inventory inventory=new Inventory();
        response=given()
                .contentType("application/json")
                .headers("x-yh-wms-whid","170952316587872256")
                .cookies(cookies)
                .body("{\"pagesize\":50,\"pageindex\":1,\"sitecode\":\""+sitecode+"\",\"skucode\":\""+skucode+"\"}")
                .post(URL+"site-inventory-center/siteinventory/list");

        String qtyonhand= response.jsonPath().getString("data.rows[0].qtyonhand");
        String qtyobdalloc=response.jsonPath().getString("data.rows[0].qtyobdalloc");
        String qtyfrozen=response.jsonPath().getString("data.rows[0].qtyfrozen");
        String qtyintransitdoc=response.jsonPath().getString("data.rows[0].qtyintransitdoc");
        String qtyintransit=response.jsonPath().getString("data.rows[0].qtyintransit");


        inventory.setQtyfrozen(Double.valueOf(qtyfrozen.toString()));
        inventory.setQtyintransit(Double.valueOf(qtyintransit.toString()) );
        inventory.setQtyintransitdoc(Double.valueOf(qtyintransitdoc.toString()) );
        inventory.setQtyobdalloc(Double.valueOf(qtyobdalloc.toString()) );
        inventory.setQtyonhand(Double.valueOf(qtyonhand.toString()) );
        inventory.setSitecode(sitecode);
        inventory.setSkucode(skucode);

        return inventory;
    }


}
